package deprecated;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;

/**
 * @ProjectName:webapp_sysmodule
 * @Description: 
 * @Copyright: Copyright (c) 2016

 * @author: 周楠
 * @date 2016年6月13日 上午9:26:10
 */
 
@Deprecated
public class ComponentExceptionResolver  extends ExceptionHandlerExceptionResolver  {
	

	private String charset="UTF-8";
	
	private String defaultExceptionpage;
	
	private Map<String,HttpMessageConverter<?>> converterMap;
	
	private Map<String,ExceptionHandler<Exception>>  handlerMap=new HashMap<String, ExceptionHandler<Exception>>(); 
	
	private List<ExceptionHandler<Exception>> reghandlers;
	
	
	public Map<String, ExceptionHandler<Exception>> getHandlerMap() {
		return handlerMap;
	}
	public void setReghandlers(List<ExceptionHandler<Exception>> reghandlers) {
		this.reghandlers = reghandlers;
	}
	public String getDefaultExceptionpage() {
		return defaultExceptionpage;
	}
	public void setDefaultExceptionpage(String defaultExceptionpage) {
		this.defaultExceptionpage = defaultExceptionpage;
	}
	
	public Map<String, HttpMessageConverter<?>> getConverterMap() {
		return converterMap;
	}
	public void setConverterMap(Map<String, HttpMessageConverter<?>> converterMap) {
		this.converterMap = converterMap;
	}
	public String getCharset() {
		return charset;
	}
	public void setCharset(String charset) {
		this.charset = charset;
	}
	
	
	public void init(){
		
		for (ExceptionHandler<Exception> handler : reghandlers) {
			
			handlerMap.put(handler.getException().getName(), handler);
			
		}
		reghandlers=null;
	
	}
	
	
	
	public ModelAndView doResolveHandlerMethodException(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod, Exception exception) {
		// TODO Auto-generated method stub
		
		if (handlerMethod == null) {  
            return null;  
        }
        Method method = handlerMethod.getMethod();  
        if (method == null) {  
            return null;  
        }  
        
        response.setCharacterEncoding(charset);
          
        //@ExceptionHandler 注解处理
        ModelAndView returnValue = super.doResolveHandlerMethodException(request, response, handlerMethod, exception);  
          
        
         if (exception!=null) {
		
        	 ExceptionHandler<Exception> handler=handlerMap.get(exception.getClass().getName()) ;
        	 
        	 if(handler!=null){
        		 ModelAndView mv= handler.doHandlerException(request, response, handlerMethod, exception);
        	    if(mv!=null){
        	    	returnValue=mv;
        	    }
        	 
        	 }
        	 
        	 
		}
		
		return returnValue;
		
	}
	
	  
      
	
	
	
	
    @SuppressWarnings({ "unchecked", "rawtypes", "unused" })  
    private ModelAndView handleResponseBody(ModelAndView returnValue, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
        Map value = returnValue.getModelMap();  
        HttpInputMessage inputMessage = new ServletServerHttpRequest(request);  
        List<MediaType> acceptedMediaTypes = inputMessage.getHeaders().getAccept();  
        
    	String requestType = request.getHeader("X-Requested-With");

		if (requestType != null && "XMLHttpRequest".equals(requestType)) acceptedMediaTypes.add(MediaType.APPLICATION_JSON);
        
        if (acceptedMediaTypes.isEmpty()) {  
            acceptedMediaTypes = Collections.singletonList(MediaType.ALL);  
        }  
        MediaType.sortByQualityValue(acceptedMediaTypes);  
        HttpOutputMessage outputMessage = new ServletServerHttpResponse(response);  
        Class<?> returnValueType = value.getClass();  
        List<HttpMessageConverter<?>> messageConverters = super.getMessageConverters();  
        if (messageConverters != null) {  
            for (MediaType acceptedMediaType : acceptedMediaTypes) {  
                for (HttpMessageConverter messageConverter : messageConverters) {  
                    if (messageConverter.canWrite(returnValueType, acceptedMediaType)) {  
                        messageConverter.write(value, acceptedMediaType, outputMessage);  
                        return new ModelAndView();  
                    }  
                }  
            }  
        }  
        if (logger.isWarnEnabled()) {  
            logger.warn("Could not find HttpMessageConverter that supports return type [" + returnValueType + "] and " + acceptedMediaTypes);  
        }  
        return new ModelAndView();  
    }  
  
}  
	